<template>
  <div class="chart-container">
    <div class="chart-header">
      <h3>借阅趋势分析</h3>
      <div class="chart-controls">
        <el-space wrap :size="16">
          <el-radio-group v-model="timeUnit" size="small">
            <el-radio-button label="day">
              <el-icon><Calendar /></el-icon>
              按日
            </el-radio-button>
            <el-radio-button label="week">
              <el-icon><Timer /></el-icon>
              按周
            </el-radio-button>
            <el-radio-button label="month">
              <el-icon><DataLine /></el-icon>
              按月
            </el-radio-button>
          </el-radio-group>
          <el-select v-model="compareType" size="small" style="min-width: 100px">
            <el-option label="同比" value="yoy" />
            <el-option label="环比" value="mom" />
          </el-select>
        </el-space>
      </div>
    </div>
    <div ref="chartRef" class="chart-content"></div>
  </div>
</template>

<script setup>
import { ref, onMounted, onUnmounted, nextTick, watch } from 'vue'
import * as echarts from 'echarts'
import { Calendar, Timer, DataLine } from '@element-plus/icons-vue'
import { getBorrowingTrend } from '@/api/statistics'

const chartRef = ref(null)
let chart = null
const timeUnit = ref('month')
const compareType = ref('yoy')

const initChart = async () => {
  await nextTick()
  if (!chartRef.value) return
  
  if (chart) {
    chart.dispose()
  }
  
  chart = echarts.init(chartRef.value)
  await updateChart()
}

const updateChart = async () => {
  if (!chart) return
  
  try {
    const response = await getBorrowingTrend(timeUnit.value, compareType.value)
    const { data } = response
    
    const option = {
      tooltip: {
        trigger: 'axis'
      },
      legend: {
        data: ['当前数据', compareType.value === 'yoy' ? '去年同期' : '上期数据']
      },
      grid: {
        left: '3%',
        right: '4%',
        bottom: '3%',
        containLabel: true
      },
      xAxis: {
        type: 'category',
        boundaryGap: false,
        data: data.xaxis
      },
      yAxis: {
        type: 'value'
      },
      series: [
        {
          name: '当前数据',
          type: 'line',
          data: data.current,
          smooth: true,
          lineStyle: {
            width: 2,
            color: '#409EFF'
          },
          areaStyle: {
            color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
              { offset: 0, color: 'rgba(64,158,255,0.3)' },
              { offset: 1, color: 'rgba(64,158,255,0.1)' }
            ])
          }
        },
        {
          name: compareType.value === 'yoy' ? '去年同期' : '上期数据',
          type: 'line',
          data: data.compare,
          smooth: true,
          lineStyle: {
            width: 2,
            color: '#67C23A'
          }
        }
      ]
    }
    
    chart.setOption(option)
  } catch (error) {
    console.error('更新图表失败:', error)
  }
}

// 监听参数变化
watch([timeUnit, compareType], () => {
  updateChart()
})

const handleResize = () => {
  chart?.resize()
}

onMounted(async () => {
  await initChart()
  window.addEventListener('resize', handleResize)
})

onUnmounted(() => {
  if (chart) {
    chart.dispose()
    chart = null
  }
  window.removeEventListener('resize', handleResize)
})
</script>

<style scoped>
.chart-container {
  padding: 20px;
  background: #fff;
  border-radius: 4px;
  width: 100%;
  height: 100%;
  position: relative;
  box-sizing: border-box;
  overflow: hidden;
}

.chart-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
  flex-wrap: wrap;
  gap: 16px;
  padding-right: 16px;
}

.chart-controls {
  display: flex;
  align-items: center;
  flex-shrink: 0;
}

.chart-content {
  width: 100%;
  height: calc(100% - 80px);
  min-height: 400px;
}

h3 {
  margin: 0;
  font-size: 18px;
  color: #303133;
  white-space: nowrap;
}

:deep(.el-radio-button__inner) {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 0 12px;
}

:deep(.el-icon) {
  margin-right: 2px;
}

:deep(.el-space) {
  flex-wrap: wrap;
}

:deep(.el-select) {
  margin-left: 8px;
}
</style> 